<template>
	<view class="index">
		<view class="logo">
			<view class="top">
				你好！
				<image src="../../static/djr.png" mode=""></image>
			</view>
			<view class="bottom">欢迎使用摩代驾</view>
		</view>
		<view class="buttonBox" v-if="mobile==''"><button type="primary" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">授权登录</button></view>
		<view class="buttonBox" v-else><button type="primary"  @click="login()">授权登录</button></view>
		<!-- <view class="input active">
      <text class="icon">&#xe605;</text>
      <input type="number" value="" maxlength="11" id="mobile" v-model="info.mobile" placeholder="请输入手机号" />
    </view>

    <view class="input active">
      <text class="icon">&#xe60d;</text>
      <input type="password" value="" id="password" v-model="info.password" placeholder="请确认密码" />
    </view>

    <view class="link">
      <view class="left" @tap="forgetThePassword()">忘记密码？</view>
      <view class="right" @tap="register()">去注册</view>
    </view>
    <button type="primary" class="button" open-type="getUserInfo" @tap="login">立即登录</button>
    <view class="to-index" @tap="toIndex()">暂不登录</view> -->
	</view>
</template>

<script>
import helper from '../../common/helper.js';
import api from '../../common/api.js';
export default {
	data() {
		return {
			focus: '',
			info: {},
			url: '/pages/index/index',
			redirectTo: false,
			mobile: '',
			driver_id: '',
			type: 1,
			code: '',
			mobile: ''
		};
	},
	onLoad(option) {
		if (option.url) {
			this.url = decodeURIComponent(option.url);
		}
		console.log(option);
		if (option.q) {
			this.driver_id = decodeURIComponent(option.q).split('driver_id=')[1];
			this.type = 2;
			if (uni.getStorageSync('__token__')) {
				uni.redirectTo({
					url: '/pages/taxiService/taxiService?driver_id=' + this.driver_id
				});
			}
		}
		if (option.redirectTo) {
			this.redirectTo = true;
		}
		this.mobile = uni.getStorageSync('mobile');
		uni.login({
			provider: 'weixin',
			success: async loginRes => {
				this.code = loginRes.code;
			}
		});
	},
	methods: {
		toIndex() {
			uni.switchTab({
				url: '/pages/index/index'
			});
		},
		onInputFocus(e) {
			let id = e.target.id;
			this.focus = id;
		},
		async decryptPhoneNumber(e) {
				var that = this;
				wx.checkSession({
					fail() {
						// session_key 已经失效，需要重新执行登录流程
						uni.login({
							provider: 'weixin',
							success: async loginRes => {
								that.code = loginRes.code;
							}
						});
					}
				});
				if (e.detail.errMsg == 'getPhoneNumber:ok') {
					// uni.login({
					// 	provider: 'weixin',
					// 	success: async loginRes => {
					// 		console.log(11111112)
					// 		this.code = loginRes.code;
					var data = {
						encryptedData: e.detail.encryptedData,
						iv: e.detail.iv,
						code: this.code
					};
					var res = await api.getUserWxPhone(data);
					if (res.code == 0) {
						console.log('获取号码');
						console.log(res);
						this.mobile = res.data.purePhoneNumber;
						uni.setStorageSync('mobile', this.mobile);
						uni.showModal({
							title: '授权用户信息',
							content: '授权用户信息可进行完整操作',
							success(res) {
								if (res.confirm) {
									that.login();
								} else if (res.cancel) {
									console.log('用户点击取消');
								}
							}
						});
					}
					console.log(res);
					// }
					// })
				} else {
					uni.showModal({
						title: '授权用户信息',
						content: '授权用户信息可进行完整操作',
						success(res) {
							if (res.confirm) {
								that.login();
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			
		},
		login() {
			// console.log(this.info);

			uni.getUserProfile({
				desc: '需要获取您的个人信息完成注册',
				success: async infoRes => {
					let userinfo = infoRes.userInfo;
					this.info.nickname = userinfo.nickName;
					this.info.avatar = userinfo.avatarUrl;
					this.info.gender = userinfo.gender;
					this.info.mobile = this.mobile;
					uni.setStorageSync('userData', userinfo);
					uni.login({
						provider: 'weixin',
						success: async loginRes => {
							console.log(222222);
							let code = loginRes.code;
							// 用code换取openid
							const ret = await api.getOpenid(loginRes.code);
							this.info.openid = ret.data.openid;

							// 登陆
							let res = await api.login(this.info);
							helper.setUserToken(res.data.userinfo.token);
							uni.hideLoading();
							helper.toast(res.msg);
							if (this.driver_id !== '') {
								uni.redirectTo({
									url: '/pages/taxiService/taxiService?driver_id=' + this.driver_id
								});
							} else {
								if (this.url) {
									uni.switchTab({
										url: this.url,
										fail() {
											uni.navigateBack({
												delta: 1,
												fail: function() {
													uni.switchTab({
														url: '/pages/index/index'
													});
												}
											});
										}
									});
								} else {
									uni.navigateBack({
										delta: 1,
										fail: function() {
											uni.switchTab({
												url: '/pages/index/index'
											});
										}
									});
								}
							}
						},
						fail: () => {
							uni.hideLoading();
							helper.toast('获取用户信息失败');
						}
					});
				},
				fail: () => {
					uni.hideLoading();
					helper.toast('获取用户信息失败');
				}
			});
		},
		register() {
			uni.navigateTo({
				url: 'register'
			});
		},
		forgetThePassword() {
			uni.navigateTo({
				url: 'forgetThePassword'
			});
		}
	}
};
</script>

<style lang="scss">
.icon {
	color: #acacac;
}
.index {
	padding: 0 10%;
}
.logo {
	margin-top: 10%;
	margin-bottom: 10%;
	.top {
		display: flex;
		align-items: flex-end;
		font-size: 80upx;
		margin-bottom: 20upx;
		image {
			width: 180upx;
			height: 160upx;
		}
	}
	.bottom {
		font-weight: 800;
		letter-spacing: 10upx;
	}
}
.input {
	display: flex;
	align-items: center;
	border-bottom: 1px #f8f8f8 solid;
	padding: 26upx 0;
	text {
		width: 10%;
	}
	input {
		width: 90%;
	}
}
.yzm {
	input {
		width: 60%;
	}
	.right {
		width: 30%;
		text-align: right;
		font-size: 28upx;
	}
}
.active {
	border-bottom: #4cd964 1px solid;
}
.link {
	margin-top: 40upx;
	font-size: 26upx;
	width: 100%;
	display: flex;
	justify-content: space-between;
}
.button {
	width: 100%;
	border-radius: 50upx;
	margin-top: 20%;
	font-size: 30upx;
	background-color: #32c45e;
	border: none;
	box-shadow: 0px 5px 10px 1px #c1d6f2;
}
.to-index {
	text-align: center;
	margin-top: 20upx;
}
</style>
